/*
 * CS_REQ_CHANNEL_RESERVE.cpp
 *
 *  Created on: 2013. 11. 21.
 *      Author: the2
 */



#include "../../Command.h"

#include "../../User.h"
#include "../../Config.h"
#include "../../NLogic/Static.h"
#include "../../constant.h"

namespace ProjectZ { namespace NCommand {

void SendFailChannelReserve(User * pUser, int16_t result)
{
	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();

	* pAck << result;
	pAck->MakePacket(CMD_SC_REQ_CHANNEL_RESERVE);
	pUser->Send(pAck);
}

void Command::CS_REQ_CHANNEL_RESERVE(User * pUser, ::Network::Packet * pPacket)
{
	/*
	/// 채널변경 예약 요청 C -> S
	CS_REQ_CHANNEL_RESERVE			0x0952
	{
		u8 channel_id;
	}

	SC_REQ_CHANNEL_RESERVE 			0x0953
	{
		s16 ack;		// 1 : 이동가능 , 2201 : ACK_CHANNEL_USER_FULL = 2201,
	}
	 */

	PREPARE_PACKET;

	uint8_t channel_id = 0;
	* pPacket >> channel_id;

	DEBUGLOG("%s channel_id %d", __FUNCTION__, channel_id);

	auto worldTree = NLogic::Static::Instance().GETWORLDSPACETREE();
	auto iter = worldTree.find(channel_id);
	if(worldTree.end() == iter)
	{
		SendFailChannelReserve(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	if(Config::InstancePtr()->GetResourceDatabase()._maxChannelUser <= iter->second->GetCCU())
	{
		SendFailChannelReserve(pUser, ACK_CHANNEL_USER_FULL);
		return;
	}

	iter->second->ReserveChannel(pUser);

	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();

	* pAck << static_cast<int16_t>(ACK_OK);
	pAck->MakePacket(CMD_SC_REQ_CHANNEL_RESERVE);
	pUser->Send(pAck);
}

} /*NCommand*/ } /*ProjectZ*/


